home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 1999 #5 / 1999 CD 5 (black).iso / Delphi3 / install / data.z / EDITFRM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-08-05  |  6.1 KB  |  257 lines

  1. unit EditFrm;
  2.  
  3. { This unit implements TEditForm, the MDI child form class, and TMemoDoc,
  4.   the automation object for a memo editor form. TMemoDoc implements the
  5.   following automated methods and properties:
  6.  
  7.   procedure Clear;
  8.     Clears the contents of the memo.
  9.  
  10.   procedure Insert(const Text: string);
  11.     Inserts the given string at the current cursor position.
  12.  
  13.   procedure Save;
  14.     Saves the contents of the memo.
  15.  
  16.   procedure Close;
  17.     Closes the memo.
  18.  
  19.   property FileName: string;
  20.     The name of the file associated with the memo. The memo can be renamed
  21.     by assigning to this property.
  22.  
  23.   property Modified: WordBool;
  24.     True if the memo has been modified since it was loaded or last saved.
  25.  
  26.   OLE Automation controllers obtain instances of TMemoDoc using the NewMemo
  27.   and OpenMemo methods of the "MemoApp.Application" OLE class. Since
  28.   instances of TMemoDoc cannot be created through OLE, there is no need to
  29.   register the class. }
  30.  
  31. interface
  32.  
  33. uses
  34.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  35.   StdCtrls, Menus, ComObj, Memo_TLB;
  36.  
  37. type
  38.   TMemoDoc = class;
  39.  
  40.   TEditForm = class(TForm)
  41.     Memo: TMemo;
  42.     MainMenu: TMainMenu;
  43.     FileMenu: TMenuItem;
  44.     FileNewItem: TMenuItem;
  45.     FileOpenItem: TMenuItem;
  46.     FileSaveItem: TMenuItem;
  47.     FileSaveAsItem: TMenuItem;
  48.     FileCloseItem: TMenuItem;
  49.     FileExitItem: TMenuItem;
  50.     procedure FormCreate(Sender: TObject);
  51.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  52.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  53.     procedure FileNewItemClick(Sender: TObject);
  54.     procedure FileOpenItemClick(Sender: TObject);
  55.     procedure FileSaveItemClick(Sender: TObject);
  56.     procedure FileSaveAsItemClick(Sender: TObject);
  57.     procedure FileCloseItemClick(Sender: TObject);
  58.     procedure FileExitItemClick(Sender: TObject);
  59.     procedure MemoChange(Sender: TObject);
  60.     procedure FormDestroy(Sender: TObject);
  61.   private
  62.     FMemoDoc: TMemoDoc;
  63.     FFileName: string;
  64.     FModified: Boolean;
  65.     FUnnamed: Boolean;
  66.     function GetOleObject: Variant;
  67.     procedure Rename(const NewName: string);
  68.     function Save(ChangeName, ForceSave: Boolean): Boolean;
  69.     procedure SaveToFile;
  70.   public
  71.     property OleObject: Variant read GetOleObject;
  72.   end;
  73.  
  74.   TMemoDoc = class(TAutoObject, IMemoDoc)
  75.   private
  76.     FEditForm: TEditForm;
  77.   protected
  78.     function Get_FileName: WideString; safecall;
  79.     function Get_Modified: WordBool; safecall;
  80.     procedure Clear; safecall;
  81.     procedure Close; safecall;
  82.     procedure Insert(const Text: WideString); safecall;
  83.     procedure Save; safecall;
  84.     procedure Set_FileName(const Value: WideString); safecall;
  85.   end;
  86.  
  87. implementation
  88.  
  89. {$R *.DFM}
  90.  
  91. uses MainFrm, ComServ;
  92.  
  93. { TEditForm }
  94.  
  95. procedure TEditForm.FormCreate(Sender: TObject);
  96. begin
  97.   FMemoDoc := TMemoDoc.Create;
  98.   FMemoDoc.FEditForm := Self;
  99.   if MainForm.NewFileName = '' then
  100.   begin
  101.     FFileName := 'Untitled.txt';
  102.     FUnnamed := True;
  103.   end else
  104.   begin
  105.     FFileName := MainForm.NewFileName;
  106.     Memo.Lines.LoadFromFile(FFileName);
  107.     FUnnamed := False;
  108.   end;
  109.   Caption := FFileName;
  110.   FModified := False;
  111. end;
  112.  
  113. procedure TEditForm.FormDestroy(Sender: TObject);
  114. begin
  115.   if FMemoDoc <> nil then
  116.   begin
  117.     FMemoDoc.FEditForm := nil;
  118.     FMemoDoc := nil;
  119.   end;
  120. end;
  121.  
  122. procedure TEditForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  123. begin
  124.   CanClose := Save(False, False);
  125. end;
  126.  
  127. procedure TEditForm.FormClose(Sender: TObject; var Action: TCloseAction);
  128. begin
  129.   Action := caFree;
  130. end;
  131.  
  132. procedure TEditForm.FileNewItemClick(Sender: TObject);
  133. begin
  134.   MainForm.FileNewItemClick(Sender);
  135. end;
  136.  
  137. procedure TEditForm.FileOpenItemClick(Sender: TObject);
  138. begin
  139.   MainForm.FileOpenItemClick(Sender);
  140. end;
  141.  
  142. procedure TEditForm.FileSaveItemClick(Sender: TObject);
  143. begin
  144.   Save(False, True);
  145. end;
  146.  
  147. procedure TEditForm.FileSaveAsItemClick(Sender: TObject);
  148. begin
  149.   Save(True, True);
  150. end;
  151.  
  152. procedure TEditForm.FileCloseItemClick(Sender: TObject);
  153. begin
  154.   Close;
  155. end;
  156.  
  157. procedure TEditForm.FileExitItemClick(Sender: TObject);
  158. begin
  159.   MainForm.Close;
  160. end;
  161.  
  162. procedure TEditForm.MemoChange(Sender: TObject);
  163. begin
  164.   FModified := True;
  165. end;
  166.  
  167. function TEditForm.GetOleObject: Variant;
  168. begin
  169.   Result := FMemoDoc as IDispatch;
  170. end;
  171.  
  172. procedure TEditForm.Rename(const NewName: string);
  173. begin
  174.   FFileName := ExpandFileName(NewName);
  175.   FUnnamed := False;
  176.   Caption := FFileName;
  177. end;
  178.  
  179. function TEditForm.Save(ChangeName, ForceSave: Boolean): Boolean;
  180. begin
  181.   Result := False;
  182.   if not ForceSave and FModified then
  183.     case MessageDlg(Format('Save changes to %s?',
  184.       [ExtractFileName(FFileName)]), mtConfirmation, mbYesNoCancel, 0) of
  185.       mrYes: ForceSave := True;
  186.       mrCancel: Exit;
  187.     end;
  188.   if ForceSave then
  189.   begin
  190.     if ChangeName or FUnnamed then
  191.       with MainForm.SaveDialog do
  192.       begin
  193.         FileName := FFileName;
  194.         DefaultExt := #0;
  195.         if not Execute then Exit;
  196.         Rename(FileName);
  197.       end;
  198.     SaveToFile;
  199.   end;
  200.   Result := True;
  201. end;
  202.  
  203. procedure TEditForm.SaveToFile;
  204. begin
  205.   Memo.Lines.SaveToFile(FFileName);
  206.   FModified := False;
  207. end;
  208.  
  209. { TMemoDoc }
  210.  
  211. procedure TMemoDoc.Clear;
  212. begin
  213.   if FEditForm <> nil then
  214.   begin
  215.     FEditForm.Memo.Clear;
  216.     FEditForm.FModified := True;
  217.   end;
  218. end;
  219.  
  220. procedure TMemoDoc.Close;
  221. begin
  222.   FEditForm.Free;
  223. end;
  224.  
  225. function TMemoDoc.Get_FileName: WideString;
  226. begin
  227.   if FEditForm <> nil then
  228.     Result := FEditForm.FFileName else
  229.     Result := '';
  230. end;
  231.  
  232. function TMemoDoc.Get_Modified: WordBool;
  233. begin
  234.   if FEditForm <> nil then
  235.     Result := FEditForm.FModified else
  236.     Result := False;
  237. end;
  238.  
  239. procedure TMemoDoc.Insert(const Text: WideString);
  240. begin
  241.   if FEditForm <> nil then FEditForm.Memo.SelText := Text;
  242. end;
  243.  
  244. procedure TMemoDoc.Save;
  245. begin
  246.   if FEditForm <> nil then FEditForm.SaveToFile;
  247. end;
  248.  
  249. procedure TMemoDoc.Set_FileName(const Value: WideString);
  250. begin
  251.   if FEditForm <> nil then FEditForm.Rename(Value);
  252. end;
  253.  
  254. initialization
  255.   TAutoObjectFactory.Create(ComServer, TMemoDoc, Class_MemoDoc, ciInternal);
  256. end.
  257.